package com.hook3.pianke;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;



import com.hook3.pianke.activity.HomeActivity;
import com.hook3.pianke.activity.MusicActivity;
import com.hook3.pianke.activity.ReadingActivity;
import com.hook3.pianke.activity.RegisterActivity;
import com.hook3.pianke.activity.SetingActivity;
import com.hook3.pianke.activity.ShopActivity;
import com.hook3.pianke.activity.SuiPianActivity;
import com.hook3.pianke.view.CommunityActivity;

import org.xutils.x;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    public Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    private LinearLayout ll_register;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        x.Ext.init(getApplication());  //xutils工具初始化
        setSupportActionBar(toolbar);
        frameLayout = (FrameLayout) findViewById(R.id.content_layout);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //
        ll_register = (LinearLayout) findViewById(R.id.ll_register);

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {


        //ll_register.setOnClickListener(new MyOnClickLinstener());
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        frameLayout.removeAllViews();
        View tag = (View) toolbar.getTag();
        toolbar.removeView(tag);
        if (id == R.id.nav_home) {
            //首页
            // Handle the camera action
            new HomeActivity(this, toolbar, frameLayout);
        } else if (id == R.id.nav_gallery) {
            //电台

            new MusicActivity(this, toolbar, frameLayout);
        } else if (id == R.id.nav_slideshow) {
            //阅读
            new ReadingActivity(this, toolbar, frameLayout);
        } else if (id == R.id.nav_manage) {
            //社区

            new CommunityActivity(this, toolbar, frameLayout);

        } else if (id == R.id.nav_share) {
            //碎片
            //toolbar.setTitle("碎片");
            new SuiPianActivity(this, toolbar, frameLayout);
        } else if (id == R.id.nav_send) {
            //良品
            toolbar.setTitle("良品");
            new ShopActivity(this, toolbar, frameLayout);
        } else if (id == R.id.nav_seting) {
            //设置

            new SetingActivity(this,toolbar,frameLayout);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        check();
        return true;
    }

    private void check() {


    }

    /**
     * 登录的点击页面
     */
    private class MyOnClickLinstener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
            startActivity(intent);

        }
    }
}
